﻿using SysCommonService.SysWin32Service.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SysCommonService.SysWin32Service
{
    public class HandelService
    {
        // 定义 EnumWindowsProc 委托
        private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
        // 引入 EnumWindows 函数
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

        // 引入 GetWindowText 函数
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        // 引入 GetClassName 函数
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        public HandelService()
        {

        }

        public List<Window> FindWindows(string keyword)
        {
            var list = new List<Window>();
            EnumWindows((hWnd, lParam) =>
            {
                StringBuilder sbTitle = new StringBuilder(256);
                GetWindowText(hWnd, sbTitle, sbTitle.Capacity);
                StringBuilder sbClass = new StringBuilder(256);
                GetClassName(hWnd, sbClass, sbClass.Capacity);
                string windowTitle = sbTitle.ToString();
                string windowClass = sbClass.ToString();
                if (windowTitle.ToLower().Contains(keyword) || windowClass.ToLower().Contains(keyword))
                {
                    string windowInfo = $"{hWnd} - {windowTitle} [{windowClass}]";
                    var window = new Window();
                    window.HandlePtr = hWnd;
                    window.WindowTitle = windowTitle;
                    window.WindowClass = windowClass;
                    list.Add(window);
                }
                return true;
            }, IntPtr.Zero);
            return list;
        }

        public string GetWindowPositon(Window window)
        {
            if (GetWindowRect(window.HandlePtr, out RECT rect))
            {
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
                return ($"窗口位置: ({rect.Left}, {rect.Top}), 尺寸: {width}x{height}");
            }
            else
            {
                return "";
            }
        }

        public string GetWindowPositon(IntPtr handle)
        {
            if (GetWindowRect(handle, out RECT rect))
            {
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
                return ($"窗口位置: ({rect.Left}, {rect.Top}), 尺寸: {width}x{height}");
            }
            else
            {
                return "";
            }
        }

    }
}
